<?php
defined('BY_JYA') or exit('error'); 
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
if(!$_SESSION['uid']){
	exi('请先登录！','error','user.login/');
}
if($_SESSION['uid'] && $_SESSION['user']['type']!=3){
	exi('只允许站长进入,超级管理员无需进入','error');
}
$return=array();
if($_RQ['op']=='get'){
	$item=pdo_get('core_agent',array('uniacid'=>$_SESSION['uniacid']));
	if(empty($item)){
		do{
			$code=random(10);
		}while(pdo_getcolumn('core_agent',array('incode'=>$code),'uniacid'));
		
		$set=core_setting('agent');
		if(!empty($set)){
			if($set['open']!=1){
				exi('代理商系统未开启','error');
			}
			if($set['check']==1){
				$check=0;
			}elseif($set['check']==0){
				$check=1;
				$checktime=TIMESTAMP;
			}
		}else{
			exi('请先设置代理商参数','error');
		}
		$data=array(
			'name'=>pdo_getcolumn('core_users',array('uniacid'=>$_SESSION['uniacid']),'name'),
			'incode'=>$code,
			'createtime'=>TIMESTAMP,
			'status'=>$check,
			'checktime'=>$checktime,
			'uniacid'=>$_SESSION['uniacid']
		);
		pdo_insert('core_agent',$data);
		$item=pdo_get('core_agent',array('uniacid'=>$_SESSION['uniacid']));
	}
	if($item['status']==1){
		$item['status']='已审核';
	}elseif($item['status']==0){
		$item['status']='待审核';
		exi('请等待管理员审核','error');
	}else{
		$item['status']='禁止';
		exi('已被禁止','error');
	}
	$item['userinfo']=pdo_get('core_users',array('uniacid'=>$_SESSION['uniacid']));
	$item['url']=manage_url('/home.welcome//fromuser='.$_SESSION['uniacid'],true);
	$return['item']=$item;
	/*$htmls=array(
		'type'=>'look',
		'desc'=>'代理商信息',
		'look'=>array(
			array('type'=>'copy','title'=>'分享链接','name'=>'url', 'help'=>'' ),
			array('type'=>'ewm','title'=>'分享二维码','name'=>'url', 'help'=>'' ),
			array('type'=>'string','title'=>'名称','name'=>'name', 'help'=>'' ),
			array('type'=>'copy','title'=>'邀请码','name'=>'incode', 'help'=>'' ),
			array('type'=>'string','title'=>'状态','name'=>'status','help'=>'' ),
			array('type'=>'time','title'=>'注册时间','name'=>'createtime' , 'help'=>''),
		)
	);
	$return['htmls']=$htmls;*/
	$url=manage_url('/home.welcome//fromuser='.$_SESSION['uniacid'],true);
	$return['h5ewm']=cfc('tools')->createqrcode($url,150,true);
	$url='/home.welcome//fromuser='.$_SESSION['uniacid'];
	$return['wxappewm']=cfc('tools')->createwxppqrcode($url,150,true);
	if(!$_RQ['ismanage']){
		set_html('header','common/header-cms');
	}
	exi($return);
}
if($_RQ['op']=='list'){
	$params=array('agent_id'=>$_SESSION['uniacid']);
	$orderby='uniacid desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$fields=array('uniacid','title');
	$total='';
	$list=pdo_getslice('core_accounts',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$return['list']=$list;
	$return['agent_num']=pdo_count('core_accounts',$params,'uniacid');
	set_html('header','common/header-cms');
	exi($return);
}
//展示页
if($_RQ['op']=='clearlog'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	//查询
	//审核状态
	if(!empty($_RQ['status'])){
		$params['status']=trim($_RQ['status']);
		$return['params']['status']=$params['status'];
	}
	//打款状态
	if(!empty($_RQ['ispay'])){
		$params['ispay']=trim($_RQ['ispay']);
		$return['params']['ispay']=$params['ispay'];
	}else{
		$params['ispay']=0;
		$return['params']['ispay']=0;
	}
	//支付订单
	if(!empty($_RQ['trade_id'])){
		$params['trade_id']=trim($_RQ['trade_id']);
		$return['params']['trade_id']=$params['trade_id'];
	}
	$fields=array('id','ordersn','money','status','ispay','createtime','pay_type','trade_id');
    $orderby='id desc';
	$total='';
	//$parameter=array('params'=>$params,'limit'=>array($pindex,$psize),'fields'=>array('id','uniacid','order_id','money','status','clearsn','createtime'));
	$list = pdo_getslice('core_agent_clear',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	if($list){
		foreach($list as &$item){

		}
	}
	$return['balance_total']=pdo_count('core_agent_clear',$params,'id');
	$return['list']=$list?$list:array();
	$return['status_xuanxiang']=array (0 => '待审核',1=>'审核通过');
	$return['ispay_xuanxiang']=array (0 => '待申请',1 => '等待打款',2=>'打款完成');
	$return['pay_type_xuanxiang']=array (0 => '支付宝支付',1 => '微信支付',2=>'银联支付');
	set_html('header','common/header-cms');
	exi($return);
}

//申请打款
if($_RQ['op']=='money'){
	
	$id=intval($_RQ['id']);

	if($id){
		pdo_update('core_agent_clear',array('ispay'=>'1'),array('id'=>$id));	
	
	exi('申请打款成功！');
	}
	else{
		
		exi('申请打款失败，请重试','error');
	}
}
?>